package com.bluesky.service.lesson009;

import com.bluesky.utils.TaskDisposeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 邮件发送
 */
@Component
@Slf4j
public class EmailSendService implements ApplicationListener<ApplicationStartedEvent>, DisposableBean {

    private boolean stop = false;

    @Autowired
    private ThreadPoolTaskExecutor emailThreadPool;


  @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
          /*
          //启动守护线程 执行邮件发送任务

        Thread thread = new Thread(()->{
            //处理邮件发送任务
            this.disposeSendEmailTask();
        },"EmailSendService");

        thread.setDaemon(true);
        thread.start();

           */
    }

    /**
     * 处理邮件发送任务
     */
    private void disposeSendEmailTask() {
        //循环处理邮件发送任务
        //循环拉取需要发送的邮件，使用线程池进行发送
        while (!stop) {
            try {
                 //获取邮件发送任务
                String task = null;
                //处理邮件发送任务
                List<String> emailListTask = this.getWaitSendEmailList();
                //使用线程池处理邮件发送
                TaskDisposeUtils.dispose(emailListTask,this::sendEmail,this.emailThreadPool);
            } catch (InterruptedException e) {
                log.error(e.getMessage(),e);
            }
        }
    }

    private void sendEmail(String email) {
        try {
             //模拟邮件发送
            TimeUnit.SECONDS.sleep(1);
             log.info("模拟邮件发送，发送邮件。。。。，{}",email);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }

    }

    private List<String> getWaitSendEmailList() {
        int count  = new Random().nextInt(500);
        if(count == 0){
            count = 100;
        }
        List<String> email = new ArrayList<>(count);
        for (int i = 0; i < count; i++){
            email.add("邮件-"+i);
        }
        return email;
    }


    @Override
    public void destroy() throws Exception {
        this.stop = true;
    }

}
